package com.example.zjx8969.myapplication;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.constraintlayout.widget.Group;

import com.copy.BaseSysParams;
import com.example.zjx8969.myapplication.beans.Student;
import com.example.zjx8969.myapplication.view.CircleAvatarView;
import com.example.zjx8969.myapplication.view.TitleLayoutView;
import com.examples.PermissionRequestFragment;
import com.examples.TimeoutClickListener;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ConstraintActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String avatarUrl = "https://profile.csdnimg.cn/F/A/2/3_love__coder";
    Button button;
    CircleAvatarView circleImageView;
    Group group;
    ConstraintLayout layout, rootView;
    private final TimeoutClickListener listener = new TimeoutClickListener(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constraint);

        TitleLayoutView titleView = findViewById(R.id.titleView);

        UIUtilsKt.addEditTitleViews(titleView, null);
/*        SettingItemView settingItemView = findViewById(R.id.settingItem);
        settingItemView.setDescriptionTextColor("10",R.drawable.icon_gem_unit);*/
        button = findViewById(R.id.btn_a);

        circleImageView = findViewById(R.id.image_avatar);
        group = findViewById(R.id.group);
        button.setOnClickListener(listener);
        findViewById(R.id.btn_b).setOnClickListener(listener);
        layout = findViewById(R.id.constraintParent);
        rootView = findViewById(R.id.rootViewId);
        new Dispatcher().dispatch(this);

    }

    private void change() {
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(layout);
        constraintSet.connect(R.id.btn_a, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        constraintSet.connect(R.id.btn_a, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT);

        constraintSet.connect(R.id.btn_b, ConstraintSet.BOTTOM, R.id.btn_a, ConstraintSet.TOP);
        constraintSet.connect(R.id.btn_b, ConstraintSet.LEFT, R.id.btn_a, ConstraintSet.LEFT);
        TransitionManager.beginDelayedTransition(layout);
        constraintSet.applyTo(layout);
    }

    public <T extends Parcelable> List<T> toList(byte[] bytes, Class<T> tClass) {
        final Parcel source = Parcel.obtain();
        source.unmarshall(bytes, 0, bytes.length);
        source.setDataPosition(0);
        final int size = source.readInt();
        if (size < 0) return null;
        List<T> list = new ArrayList<>();
        try {
            final Field declaredField = tClass.getDeclaredField("CREATOR");
            final Parcelable.Creator creator = (Parcelable.Creator) declaredField.get(null);
            for (int i = 0; i < size; i++) {
                final T fromParcel = (T) creator.createFromParcel(source);
                list.add(fromParcel);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<String> toList(byte[] bytes) {
        final Parcel source = Parcel.obtain();
        source.unmarshall(bytes, 0, bytes.length);
        source.setDataPosition(0);
        final int size = source.readInt();
        if (size < 0) {
            source.recycle();
            return null;
        }
        List<String> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(source.readString());
        }
        source.recycle();
        return list;
    }

    public byte[] parcel2Bytes(List<String> data) {
        final Parcel source = Parcel.obtain();
        if (data == null) {
            source.writeInt(-1);
        } else {
            source.writeInt(data.size());
            for (String t : data) {
                source.writeString(t);
            }
        }
        final byte[] marshall = source.marshall();
        source.recycle();
        return marshall;
    }

    protected String byteArrayToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte element : bytes) {
            int v = element & 0xff;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString().toUpperCase(Locale.US);
    }

    protected byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }

/*    public String transform(Parcelable parcelable) {
        final Parcel obtain = Parcel.obtain();
        parcelable.writeToParcel(obtain, parcelable.describeContents());
        final byte[] marshall = obtain.marshall();

        return byteArrayToHexString(marshall);
    }*/

    public <T extends Parcelable> T transform(String parcelable, Class<T> tClass) {
        final byte[] bytes = hexStringToByteArray(parcelable);

        return null;
    }

    public void onHeightClicked(View view) {
        Student student = new Student("hawking", 19);
        circleImageView.setBitmap(avatarUrl);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_a:
/*                for (int i = 0; i < 500; i++) {
                    BaseSysParams sysParams = BaseSysParams.BaseParameter.getInstance().obtain(i);
                    Log.d("tag", "i = " + i + "," + sysParams.hashCode() + "," + sysParams.toJson());
                }*/
                BaseSysParams sysParams = BaseSysParams.BaseParameter.getInstance().obtain(234);
//                sysParams.clone();
//                textUpon();
//                adjustUpon(R.id.titleText, R.id.image_icon);
                /*Intent intents = new Intent(this, OnBoardingActivity.class);
                ActivityResultFragment.getInstance(intents).onSuccess(this, intent1 -> {
                    Toast.makeText(ConstraintActivity.this, "onSuccessCalled", Toast.LENGTH_LONG).show();
                    return null;
                });*/
                break;
            case R.id.btn_b:
                PermissionRequestFragment.getInstance(Manifest.permission.READ_CONTACTS, Manifest.permission.READ_PHONE_STATE)
                        .onFailure(strings -> {
                            for (int i = strings.size() - 1; i >= 0; i--) {
                                Log.d("tag", strings.get(i) + " " + i);
                            }
                            return null;
                        }).onSuccess(unit -> {
                    Log.d("tag", "permission all granted");
                    return null;
                }).request(getSupportFragmentManager());
//                adjustUpon(R.id.image_icon, R.id.titleText);

                Log.d("tag", getSupportFragmentManager().getFragments().size() + "");
//                imageUpon();
                break;
            default:
                Log.e("tag", "onclick" + v.getId());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.d("tag", "activity:onActivityResult:" + requestCode);
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void textUpon() {
        int textId = R.id.titleText, imageId = R.id.image_icon;
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(rootView);
        constraintSet.connect(textId, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        constraintSet.connect(textId, ConstraintSet.BOTTOM, imageId, ConstraintSet.TOP);

        constraintSet.connect(imageId, ConstraintSet.TOP, textId, ConstraintSet.BOTTOM);
        constraintSet.connect(imageId, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        TransitionManager.beginDelayedTransition(rootView);
        constraintSet.applyTo(rootView);
    }

    private void imageUpon() {
        int textId = R.id.titleText, imageId = R.id.image_icon;
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(rootView);
        constraintSet.connect(imageId, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        constraintSet.connect(imageId, ConstraintSet.BOTTOM, textId, ConstraintSet.TOP);

        constraintSet.connect(textId, ConstraintSet.TOP, imageId, ConstraintSet.BOTTOM);
        constraintSet.connect(textId, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        TransitionManager.beginDelayedTransition(rootView);
        constraintSet.applyTo(rootView);
    }

    private void adjustUpon(int uponId, int bottomId) {
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(rootView);
        constraintSet.connect(uponId, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        constraintSet.connect(uponId, ConstraintSet.BOTTOM, bottomId, ConstraintSet.TOP);

        constraintSet.connect(bottomId, ConstraintSet.TOP, uponId, ConstraintSet.BOTTOM);
        constraintSet.connect(bottomId, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        TransitionManager.beginDelayedTransition(rootView);
        constraintSet.applyTo(rootView);
    }

    private void changeReverse() {
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(layout);
        constraintSet.connect(R.id.btn_b, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        constraintSet.connect(R.id.btn_b, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT);
        constraintSet.connect(R.id.btn_a, ConstraintSet.BOTTOM, R.id.btn_b, ConstraintSet.TOP);
        constraintSet.connect(R.id.btn_a, ConstraintSet.LEFT, R.id.btn_b, ConstraintSet.LEFT);
        TransitionManager.beginDelayedTransition(layout);
        constraintSet.applyTo(layout);
    }

}
